package com.qiwo;

import com.qiwo.common.config.ISiteConfig;
import com.qiwo.common.utils.SpringContextHolder;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.servlet.MultipartConfigElement;

/**
 * 程序入口
 * @author chenwenchao
 * @date 2018-12-15
 */
@EnableTransactionManagement
@ServletComponentScan
@MapperScan("com.qiwo.*.dao")
@SpringBootApplication
public class Application  extends SpringBootServletInitializer {
	private static Logger log = LoggerFactory.getLogger(Application.class);
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		printProjectConfigs();
	}
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		// 注意这里要指向原先用main方法执行的Application启动类
		return builder.sources(Application.class);
	}
	/**
	 * 文件上传配置
	 *
	 * @return
	 */
	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		//  单个数据大小// KB,MB
		factory.setMaxFileSize("30MB");
		/// 总上传数据大小
		factory.setMaxRequestSize("300MB");
		return factory.createMultipartConfig();
	}
	private static void printProjectConfigs() {
		ServerProperties serverProperties = SpringContextHolder.getApplicationContext().getBean(ServerProperties.class);
		DataSourceProperties dataSourceProperties = SpringContextHolder.getApplicationContext().getBean(DataSourceProperties.class);
		ISiteConfig config = SpringContextHolder.getApplicationContext().getBean(ISiteConfig.class);
		log.info("开启演示模式：" + config.isDemoMode());
		log.info("开启调试模式：" + config.isDevMode());
		log.info("数据库：" + dataSourceProperties.getUrl());
		log.info("==================> run at http://localhost:" + serverProperties.getPort() + "/main"+"<==================");
	}
}
